/**
 * \file AuthActivity.java
 * \author Nicolaj Quottrup
 */
package com.rootbat.fleamarketapp.authorization;

import com.rootbat.fleamarketapp.android.R;
import com.rootbat.fleamarketapp.authorization.ServerService.LocalBinder;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;

/**
* \brief Activity to handle authorization module.
*/
public class AuthActivity extends FragmentActivity implements IServerFragmentFunctions {
		// Bound state
		public boolean mBound = false;
		// Bound service
		public ServerService mService;
		
	    /// <summary>
	    /// onCreate - Sets up Authorization activity view and adds corresponding fragment wanted visible and binds service
	    /// </summary>
	    @Override
	    public void onCreate(Bundle savedInstanceState) 
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_auth);
	        
	        if(findViewById(R.id.auth_fragment_container) != null) {
	        	
	        	if(savedInstanceState != null) {
	        		return;
	        	}
	        	// Bind serverservice
			    Intent intents = new Intent(this, ServerService.class);
			    bindService(intents, mConnection, Context.BIND_AUTO_CREATE);
	        	
	        	// Get starting intent variable for check on necessary fragment start
	        	Intent intent = getIntent();
	        	String str = intent.getStringExtra("Fragment");
	        	if(str == "Login")
	        	{
	        		// If "Login" use login fragment and add to container.
		        	Auth_Login_Fragment fragLog = new Auth_Login_Fragment();
		            getSupportFragmentManager().beginTransaction().replace(R.id.auth_fragment_container, fragLog).commit();
	        	}
	        	else if(str == "Register")
	        	{
	        		// If "Register" use Register fragment and add to container.
	        		Auth_Register_Fragment fragReg = new Auth_Register_Fragment();
	        		getSupportFragmentManager().beginTransaction().replace(R.id.auth_fragment_container, fragReg).commit();
	        	}
	        	else
	        	{
	        		// If nothing defined, use login fragment as default.
		        	Auth_Login_Fragment rem = new Auth_Login_Fragment();
		            getSupportFragmentManager().beginTransaction().replace(R.id.auth_fragment_container, rem).commit();
	        	}
	        }
	    }
	    
	    /// <summary>
	    /// onDestroy - Unbind serverservice service.
	    /// </summary>
	    @Override
	    protected void onDestroy() {
		    super.onDestroy();
	        if (mBound) {
	            unbindService(mConnection);
	            mBound = false;
	        }
	    }
	    
	    /// <summary>
	    /// getServerService - return activity's serverservice to fragment 
	    /// </summary>
		@Override
		public ServerService getServerService() {
			return mService;
		}
		
	    /// <summary>
	    /// mConnection - Internal class connection for use in service binding.
	    /// </summary>
		private ServiceConnection mConnection = new ServiceConnection() {

		    /// <summary>
		    /// onServiceConnected - set bound service object on bind
		    /// </summary>
	        @Override
	        public void onServiceConnected(ComponentName className, IBinder service) {
	            LocalBinder binder = (LocalBinder) service;
	            mService = binder.getService();
	            mBound = true;
	        }

		    /// <summary>
		    /// onServiceDisconnected - on unbind set bound to false
		    /// </summary>
	        @Override
	        public void onServiceDisconnected(ComponentName arg0) {
	            mBound = false;
	        }
	    };
}
